package com.jml.蚂蚁.netty.粘包拆包client;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * @author:jml
 * @date: 2019/12/24 11:03
 * @description：
 */
public class ClientHandler extends SimpleChannelInboundHandler {
	
	
	/**
	 * @Description:活跃的通道,客户端发送消息
	 * @param ctx
	 * @return:void
	 */
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		// 我们现在客户端发送10条消息，那么我们客户端也要收到这10条消息
		for (int i = 0; i < 10; i++) {
			ctx.writeAndFlush((Unpooled.copiedBuffer("客户端发送消息\n", CharsetUtil.UTF_8)));
		}
	}
	
	/**
	 * @Description:读取消息
	 * @param ctx
	 * @param msg
	 * @return:void
	 */
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
		ByteBuf byteBuf = (ByteBuf) msg;
		String resp = byteBuf.toString(CharsetUtil.UTF_8);
		System.out.println("客户端获取服务器端响应的消息：" + resp);
	}
}
